function ajaxRequest(type, url, data, success, error, token) {
    $.ajax({
        type: type,
        url: url,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        headers: {
            "X-CSRFToken": token
        },
        data: data,
        success: success,
        error: error
    })
}

function ajaxRequestJson(type, url, data, success, error, token) {
    $.ajax({
        type: type,
        url: url,
        contentType: "application/json",
        headers: {
            "X-CSRFToken": token
        },
        data: JSON.stringify(data),
        success: success,
        error: error
    })
}


function GetQueryString(key) {
    // 获取参数
    var url = window.location.search;
    // 正则筛选地址栏
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : null;
}

Date.prototype.Format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

//html2canvas的方法
function convert2canvas(shootDom, targetDom, dutyId) {
    var $d = $.Deferred();
    var shareContent = shootDom;//需要截图的包裹的（原生的）DOM 对象
    var width = shareContent.offsetWidth; //获取dom 宽度
    var height = shareContent.offsetHeight; //获取dom 高度
    var canvas = document.createElement("canvas"); //创建一个canvas节点
    var scale = 2; //定义任意放大倍数 支持小数
    canvas.width = width * scale; //定义canvas 宽度 * 缩放
    canvas.height = height * scale; //定义canvas高度 *缩放
    canvas.getContext("2d").scale(scale, scale); //获取context,设置scale
    var opts = {
        scale: scale, // 添加的scale 参数
        canvas: canvas, //自定义 canvas
        // logging: true, //日志开关，便于查看html2canvas的内部执行流程
        width: width, //dom 原始宽度
        height: height,
        async: false,
        useCORS: true // 【重要】开启跨域配置
    };
    html2canvas(shareContent[0], opts).then(function (canvas) {

        var context = canvas.getContext('2d');
        // 【重要】关闭抗锯齿
        context.mozImageSmoothingEnabled = false;
        context.webkitImageSmoothingEnabled = false;
        context.msImageSmoothingEnabled = false;
        context.imageSmoothingEnabled = false;

        // 【重要】默认转化的格式为png,也可设置为其他格式
        var img = Canvas2Image.convertToJPEG(canvas, canvas.width, canvas.height);
        img.onload = function () {
            $(img).css({
                "width": canvas.width / 2 + "px",
                "height": canvas.height / 2 + "px"
            }).attr("data-dutyId", dutyId);
            $d.resolve();
        };

        targetDom.html(img);

    });
    return $d.promise();
}

function loadPaginator(link, per_page, target, data, list) {
    //加载分页
    var pagi_str = '<nav aria-label="Page navigation">' +
        '<div class="showPageItemsBox pull-left"><label class="col-sm-3 control-label" style="padding-top: 5px;">显示</label><div class="col-sm-5"><select class="form-control col-sm-4">';
    //加载下拉列表
    for (var n = 0; n < list.length; n++) {
        if (parseInt(per_page) === list[n]) {
            pagi_str += '<option value="' + list[n] + '" selected>' + list[n] + '</option>';
        } else {
            pagi_str += '<option value="' + list[n] + '">' + list[n] + '</option>';
        }
    }
    pagi_str += '</select></div><label class="col-sm-4 control-label" style="padding-top: 5px;">条数据</label>' + '</div>' +
        '<ul class="pagination pull-right">';
    if (!data.page.has_prev) {//没有前一页
        pagi_str += '<li class="disabled">';
        pagi_str += '<span><span aria-hidden="true">&laquo;</span></span></li>';
    } else {
        pagi_str += '<li>';
        pagi_str += '<a href="' + link + '?page=' + (data.page.page - 1) + '&per_page=' + per_page + '" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
    }
    for (var m = 0; m < data.page.page_list.length; m++) {
        var page_item = data.page.page_list[m];
        if (page_item !== null) {
            if (page_item === data.page.page) {
                pagi_str += '<li class="active"><a href="' + link + '?page=' + page_item + '&per_page=' + per_page + '">' + page_item + '</a></li>';
            } else {
                pagi_str += '<li><a href="' + link + '?page=' + page_item + '&per_page=' + per_page + '">' + page_item + '</a></li>';
            }
        }else{
            pagi_str += '<li><span class="pull-left">...</span></li>';
        }


    }
    if (!data.page.has_next) {//当前没有下一页
        pagi_str += '<li class="disabled">';
        pagi_str += '<span><span aria-hidden="true">&raquo;</span></span></li>';
    } else {
        pagi_str += '<li>';
        pagi_str += '<a href="' + link + '?page=' + (data.page.page + 1) + '&per_page=' + per_page + '" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li></ul></nav>';
    }
    $(target).html(pagi_str);
}

//选择分页显示条目数量:


//公共变量
var GLOBAL_VARIABLE = {
    "BASE_URL": ""
};
$(function () {
    var pathname = window.location.pathname;
    //导航栏右上角
    $(document).on("click", function (event) {
        var target = event.target;
        if ($(target).closest(".personal-box").hasClass("personal-box")) {
            $(".dropdown-menu-box").toggleClass("hidden");
            event.stopPropagation();
        } else if (!$(target).closest(".dropdown-menu-box").hasClass("dropdown-menu-box") && !$(target).closest(".personal-box").hasClass("personal-box")) {
            $(".dropdown-menu-box").addClass("hidden");
            event.stopPropagation();
        }

        //系统管理---菜单切换
        if ($(target).hasClass("nav-menu-item")) {
            //先去除同类元素的active类名
            $(target).closest(".nav-menu").find(".nav-menu-item").each(function () {
                $(this).remove(".active");
            });
            //为当前路由添加点击样式
            $(target).addClass("active");
        }
    });
    //页面刷新判断是哪个路由
    judgeNav();
    function judgeNav() {
        $(".nav-menu .nav-menu-item").each(function () {
            var url = $(this).attr("href");
            if (pathname.indexOf(url) > -1) {
                $(this).addClass("active");
            }
        });
    }
});